package com.hyk.commonLib.common.utils.netRequest;

import android.app.Activity;
import android.view.View;

import com.hyk.commonLib.common.activity.BaseActivity;
import com.hyk.commonLib.common.utils.ViewUtils;
import com.trello.rxlifecycle4.LifecycleTransformer;

import io.reactivex.rxjava3.core.ObservableTransformer;

/**
 * Created by 贺玉琨 on 2019/3/6 0006.
 */
public class NetUtils {
    private static INetUtils iNetUtils;

    public static void setNetUtilsImpl(INetUtils iNetUtils) {
        NetUtils.iNetUtils = iNetUtils;
    }

    public static boolean netUtilsImplIsEmpty() {
        return NetUtils.iNetUtils == null;
    }

    public static <T> T getServerInstance(Class<T> server) {
        return iNetUtils.getServerInstance(server);
    }

    public static <T> ObservableTransformer<T, T> schedulerTransformer() {
        return iNetUtils.getCompose();
    }

    public static <T> ObservableTransformer<T, T> schedulerTransformer(LifecycleTransformer<T> transformer) {
        return iNetUtils.getCompose(transformer);
    }

    public static <T> ObservableTransformer<T, T> schedulerTransformer(View v) {
        Activity activity = ViewUtils.getActivity(v);
        if (activity instanceof BaseActivity) {
            return iNetUtils.getCompose(((BaseActivity<?>) activity).bindUntilDestroy());
        }
        return iNetUtils.getCompose();
    }
}